/**
 * JAVACC DEMO 1.0
 * @copy right dwusoft company All rights reserved. 
 */
package com.apache.order.service.plugins;

import com.apache.api.manager.PluginConnector;
import com.apache.api.vo.ParamsVo;
import com.apache.api.vo.ResultEntity;
import com.apache.cache.util.Validator;
import com.apache.order.entity.TOrder;
import com.apache.order.manager.TOrderManager;
import com.apache.order.util.MessageUtil;
import com.apache.tools.StrUtil;

/**
 * description:  数量获取插件
 * @author Hou Dayu 创建时间：2015-8-27  
 */
public class CountOrderPluginImpl implements PluginConnector {

	private TOrderManager tOrderManager;

	/**
	 * TODO 简单描述该方法的实现功能（可选）.  
	 * @see com.dwusoft.api.manager.PluginConnector#execute(com.dwusoft.api.vo.ParamsVo)  
	 */
	public Object execute(ParamsVo vo) throws Exception {

		ResultEntity entity = new ResultEntity();
		entity.setResult("true");
		TOrder main = new TOrder();
		main = (TOrder) MessageUtil.convert2Bean(vo.getParams(), main);
		String orderStatuses = String.valueOf(vo.getParams("orderStatuses"));
		ParamsVo<TOrder> pv = new ParamsVo<TOrder>();
		String channels = String.valueOf(vo.getParams("channels"));
		if (StrUtil.isNotNull(channels)) {
			String str[] = channels.split(",");
			String strs = "";
			for (int i = 0; i < str.length; i++) {
				if (i == str.length - 1) {
					strs += "'" + str[i] + "'";
				} else {
					strs += "'" + str[i] + "',";
				}
			}
			pv.setParams("channels", strs);
		}
		if (Validator.isNotNull(orderStatuses)) {
			pv.setParams("orderStatuses", orderStatuses);
		}
		if (Validator.isNotNull(main.getBuyUserEname()) && Validator.isNotNull(main.getSellUserEname())) {
			pv.setMethodKey("ByPropertyBuyOrSell");
		}
		pv.setObj(main);
		long count = tOrderManager.countInfo(pv);
		entity.setEntity(count);
		entity.setMessage("查询成功");
		return entity;
	}

	private String getParamStr(ParamsVo vo, String attrName) {
		return Validator.getDefaultStr(String.valueOf(vo.getParams(attrName)), "");
	}

	public void settOrderManager(TOrderManager tOrderManager) {
		this.tOrderManager = tOrderManager;
	}

}
